package com.fuse.thehype;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class HypeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}





/*
public class HelloworldActivity extends Activity {
    
    public void UpdateData(final String value) {
    	this.runOnUiThread(new Runnable() {
    		public void run() {
	    		EditText databox = (EditText) findViewById(R.id.editTextData);
			    databox.setText(value);
    		}
		});
    }
    
    public void GetFunction(View view) {
    	UpdateData("Loading...");
    	new Thread() {
    		public void run() {
		    	Log.d("myapptag", "GetFunction!");
		    	EditText namebox = (EditText) findViewById(R.id.editTextName);
		    	
		    	String url = "http://fuse2011-demo.appspot.com/getdata?name=" + URLEncoder.encode(namebox.getText().toString());
		
		    	HttpClient httpClient = new DefaultHttpClient();
		    	HttpGet httpGet = new HttpGet("http://fuse2011-demo.appspot.com/getdata?name=" + URLEncoder.encode(namebox.getText().toString()));
		    	
		    	Log.d("myapptag", "URL: " + url);
		    	
		    	try {
		    		InputStream is = httpClient.execute(httpGet).getEntity().getContent();
		    		
		    		// Chunk of (ugly java) code to get string
		    		BufferedReader reader = new BufferedReader(new InputStreamReader(is), 512);
		    		StringBuilder sb = new StringBuilder();
		    		char[] buffer = new char[512];
		    		int read;
		    		while ((read = reader.read(buffer)) != -1)
		    			sb.append(buffer, 0, read);
		    		
		    		String result_string = sb.toString();
		    		Log.i("myapptag", result_string);
		    		
		    		String displaydata = namebox.getText().toString() + ":\n";
		    		UpdateData(displaydata);
		    		
		    		JSONObject result = (JSONObject) new JSONTokener(result_string).nextValue();
		    		JSONArray list = result.getJSONArray("data");
		    		for(int i = 0; i < list.length(); i++) {
		    			JSONObject obj = list.getJSONObject(i);
		    			Log.i("myapptag", obj.getString(namebox.getText().toString()));
		    			displaydata += obj.getString(namebox.getText().toString()) + "\n";
		    		}
		    		
		    		UpdateData(displaydata);
		    	} catch (IllegalStateException e) {
		    		Log.e("myapptag", Log.getStackTraceString(e));
		    	} catch (ClientProtocolException e) {
		    		Log.e("myapptag", Log.getStackTraceString(e));
		    	} catch (IOException e) {
		    		Log.e("myapptag", Log.getStackTraceString(e));
		    	} catch (JSONException e) {
		    		Log.e("myapptag", Log.getStackTraceString(e));
		    	}
    		}
    	}.start();
    }
    
    public void PutFunction(View view) {
    	Log.d("myapptag", "PutFunction!");
    	UpdateData("Loading...");
    	
    	EditText namebox = (EditText) findViewById(R.id.editTextName);
    	EditText infobox = (EditText) findViewById(R.id.editTextInfo);
    	
    	final String url = "http://fuse2011-demo.appspot.com/putdata?name=" + URLEncoder.encode(namebox.getText().toString()) +
    					"&info=" + URLEncoder.encode(infobox.getText().toString());
    	new Thread() {
    		public void run() {
		
		    	HttpClient httpClient = new DefaultHttpClient();
		    	HttpGet httpGet = new HttpGet(url);
		    	
		    	Log.d("myapptag", "URL: " + url);
		    	
		    	try {
		    		InputStream is = httpClient.execute(httpGet).getEntity().getContent();
		    		
		    		// Chunk of (ugly java) code to get string
		    		BufferedReader reader = new BufferedReader(new InputStreamReader(is), 512);
		    		StringBuilder sb = new StringBuilder();
		    		char[] buffer = new char[512];
		    		int read;
		    		while ((read = reader.read(buffer)) != -1)
		    			sb.append(buffer, 0, read);
		    		
		    		UpdateData(sb.toString());
		    	} catch (IllegalStateException e) {
		    		Log.e("myapptag", Log.getStackTraceString(e));
		    	} catch (ClientProtocolException e) {
		    		Log.e("myapptag", Log.getStackTraceString(e));
		    	} catch (IOException e) {
		    		Log.e("myapptag", Log.getStackTraceString(e));
		    	}
    		}
    	}.start();
    }
}

*/